﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;
using RENIT.DragNDropUpload.Utils;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using RENIT.DragNDropUpload.Extensibility;

namespace RENIT.DragNDropUpload.Controls
{
    public class PageUploadsManager : UserControl
    {
        private bool? pageIsUsable = null;
        private List<UploadTargetData> targets;

        public string ProgressMode
        {
            get
            {
                string result = "DEFAULT";
                if (Context.User != null)
                {
                    if (Context.User.Identity != null)
                    {
                        if (!string.IsNullOrEmpty(Context.User.Identity.AuthenticationType))
                        {
                            result = "NTLM";
                        }
                        else
                        {
                            result = "CLASSIC";
                        }
                    }
                }
                return result;
            }
        }

        protected List<UploadTargetData> UploadTargets
        {
            get 
            {
                if (targets == null)
                {
                    targets = WebPartsAnalizer.GetUploadTargets(Page);
                }
                return targets;
            }
        }

        public bool PageIsUsable
        {
            get 
            {
                if (!pageIsUsable.HasValue)
                {
                    pageIsUsable = DetectForPageDocLibWebParts();
                }
                return pageIsUsable.GetValueOrDefault();
            }
        }

        protected string TargetsJSON
        {
            get 
            {
                var dict = new Dictionary<string, UploadTargetData>();
                foreach (var target in targets)
                {
                    if (!dict.ContainsKey(target.WPCellID))
                    {
                        dict.Add(target.WPCellID, target);
                    }
                    else
                    {
                        System.Diagnostics.Trace.WriteLine(string.Format("Duplicate webparts Qualfiers somehow: " + target.WPCellID));
                    }
                }
                return SimpleJSON.Create(dict).Serialize();
            }
        }

        private bool DetectForPageDocLibWebParts()
        {
            return UploadTargets.Count > 0;
        }

        public SimpleJSON UploaderLimitsJSON
        {
            get 
            {
                var app = SPContext.Current.Site.WebApplication;
                return new SimpleJSON().Set("MaxFileSize", app.MaximumFileSize)
                    .Set("BlockedFileTypes", SimpleJSON.Create(app.BlockedFileExtensions.ToArray()));
            }
        }


    }
}
